# -*- encoding=utf8 -*-
__author__ = "GQ"

#环境安装：  pip install pocoui
from airtest.core.api import *
from airtest.cli.parser import cli_setup
import random

if not cli_setup():
#     auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/c9a55ee3?cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=MAXTOUCH",]) # 小米9
    auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/PWHNW19919003324?cap_method=MINICAP_STREAM&&ori_method=MINICAPORI&&touch_method=MINITOUCH",]) # 华为navo
#    auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/668e99a4?cap_method=MINICAP_STREAM&&ori_method=MINICAPORI&&touch_method=MINITOUCH",]) # 华为navo



from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

size=poco.get_screen_size()
width = size[0]
height = size[1]
# print("获取到设备分辨率大小："+ str(size))
# print("下："+ str(0.5*width)+' x '+str(0.8*height))
# print("上："+ str(0.5*width)+' x '+str(0.1*height))

sleep_time=1
long_sleep_time=8
play_time=10
silp_list_time=2
silp_video_time=30
snapshot_time=3

# script content
print("start...")

comment_content=["真不错!","太好看了。","非常精彩的视频！",
                "那几秒的邂逅，像极了爱情。"
,"我们总以为来日方长，却忘了世事无常."
,"不生不灭无魂无魄，只有一具不朽的躯壳。"
,"我这一生啊平庸至极，唯独见你，波澜迭起。"
,"大风带走了树叶，只留下了树，树说没事，只有树叶是真的高兴。"
,"男孩怀念那个满是起风了，纸短情长和往后余生的夏天，以及满是成都的秋天，还有就是你一定要幸福啊!"
,"可惜的就是说散就散，一起吹过晚风的人终究还是会分开...."
,"我喜欢可乐，去商店买发现没有了，就随手拿了一瓶牛奶。结账时突然发现了可乐，可是我不想换了。"
,"因为有爱，才会有期待，所以纵使失望，也是一种幸福，虽然这种幸福有点痛。9.枕头要常晒,因为里面装满了心酸的泪和发霉的梦。"
,"成熟的人钱字当头，幼稚的人感情入喉!"]

close_phone=poco("com.android.systemui:id/notification_panel")
phone_banner=poco("com.huawei.android.launcher:id/layout")
sign_icon=poco("com.leba.strlive:id/rl_btn_sign_in")
sign_close=poco("android:id/content").offspring("com.leba.strlive:id/btn")
live_video_title=poco("com.leba.strlive:id/tab_layout").child("android.widget.TextView")
home_bar=poco("com.leba.strlive:id/btn_home")
attention=poco("com.leba.strlive:id/iv_attention") #关注
# comment=poco("com.leba.strlive:id/btn_comment_2") #评论
comment=poco("com.leba.strlive:id/iv_comment")
comment_edit=poco("com.leba.strlive:id/comment_edit") #评论编辑
comment_send=poco("com.leba.strlive:id/btn_send") #发送评论
video_status=poco("com.leba.strlive:id/tv_status")
share=poco("com.leba.strlive:id/iv_share") #分享视频
is_attention=poco("com.leba.strlive:id/ll_attention") #已关注
live_choose=poco("com.leba.strlive:id/btn_live_choose")
me_element=poco("com.leba.strlive:id/btn_me")
a_video_thumbnail=poco("com.leba.strlive:id/view_pager").offspring("com.leba.strlive:id/recyclerView").child("android.widget.RelativeLayout")
click_zan=poco("com.leba.strlive:id/iv_zan") # 点赞
# 视频红包元素
red_open=poco("com.leba.strlive:id/iv_tips")
red_close=poco("com.leba.strlive:id/iv_close")
red_detail=poco("com.leba.strlive:id/rl_detail")
red_continue=poco("com.leba.strlive:id/tv_continue")
# red_receive=poco("com.leba.strlive:id/rl_progress")
red_receive=poco("android.widget.LinearLayout").offspring("com.leba.strlive:id/rl_progress")
#poco("android.widget.LinearLayout").offspring("com.leba.strlive:id/replaced").offspring("com.leba.strlive:id/viewPager").offspring("com.leba.strlive:id/circle_progress")

# '''上滑解锁'''
keyevent('POWER')
swipe(v1=[0.5*width,0.85*height],v2=[0.5*width,0.2*height]) 

# ''' 打开享乐吧,滑动到视频主页  '''
start_app("com.leba.strlive")

''' TODO: 登录不同账号 不同沉浸式 '''
while not home_bar.exists():sleep(long_sleep_time)

while not (sign_icon.exists() or live_video_title.exists()):sleep(long_sleep_time)
if sign_icon.exists(): sign_icon.click();sleep(sleep_time)
if sign_close.exists(): sign_close.click();sleep(sleep_time)
if live_video_title[1].exists():live_video_title[1].click();sleep(sleep_time) # 点击视频title切换到视频主页
##swipe(v1=[0.95*width,0.5*height],v2=[0.25*width,0.5*height])  # 左滑动到视频

# 下拉刷新
for i in range(silp_list_time):
    swipe(v2=[0.5*width,0.8*height],v1=[0.5*width,0.2*height]) #下拉刷新会截屏，第一帧
    sleep(3)
sleep(3)


#  ##测试视频播放
# # 点击播放视频，上滑加载
if a_video_thumbnail.exists() : touch([0.3*width,0.4*height])
for i in range(silp_video_time):
#     todo: 进入直播间的兼容
    if video_status.exists(): swipe(v1=[0.5*width,0.7*height],v2=[0.5*width,0.07*height]) # 视频异常状态，上滑跳过
    sleep(play_time)
    if red_receive.exists():red_receive.click()
    if red_open.exists():red_open.click();sleep(sleep_time)
    #if red_open.exists() & (i%2==1):red_open.click();sleep(sleep_time)
#     if red_close.exists():red_close.click();sleep(sleep_time)
    if red_continue.exists():red_continue.click();sleep(sleep_time)
#     if red_receive.exists():red_receive.click()
    swipe(v1=[0.5*width,0.7*height],v2=[0.5*width,0.07*height]) #上滑会截屏，第一帧
#     swipe(v1=[550, 1866],v2=[550, 100])
    sleep(sleep_time) # 停留一下防止视频切换时找不到元素
    
    #点赞 
    if i%5==0: 
        if click_zan.exists():click_zan.click() #点赞 
    if i%6==0: touch([0.35*width,0.3*height],times=2) # 双击点赞 
    # 关注
    if i%9==0:
        sleep(sleep_time) # 等待UI加载
        if attention.exists(): 
            attention.click()
#     if is_attention.exists(): keyevent('BACK')
    # 评论
    if i%8==0:
        sleep(sleep_time);comment.click();sleep(sleep_time)
        comment_edit.click();sleep(sleep_time)
        text(random.choice(comment_content));sleep(sleep_time)
        comment_send.click();sleep(sleep_time)
        touch([0.5*width,0.3*height])


    
keyevent("BACK")
stop_app("com.leba.strlive")
keyevent('POWER')


#生成测试报告 generate html report
from airtest.report.report import simple_report
from time import strftime,gmtime,localtime
date=strftime(r"%Y-%m-%d_%H_%M_%S",localtime())
expression=__file__.rfind('.')
report_path=__file__[:expression]+'_'+date+'.html'
simple_report(__file__, logpath=True,output=report_path)
#simple_report(__file__, logpath=True)